package com.brillilab.service.core.experiments.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.experiments.PlateMapper;
import com.brillilab.domain.enums.common.BoolEnum;
import com.brillilab.domain.po.experiments.Plate;
import com.brillilab.service.core.experiments.IPlateService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class PlateServiceImpl implements IPlateService {

    @Resource
    private PlateMapper plateMapper;

    @Override
    public List<Plate> selectListByExperimentIdAndLtSort(Long experimentId,Integer sort) {
        return plateMapper.selectExperimentCanChooseList(experimentId,sort);
    }

    @Override
    public boolean insert(Plate plate) {
        return plateMapper.insert(plate) > 0;
    }

    @Override
    public Plate selectById(Long plateId) {
        return plateMapper.selectOne(
                new LambdaQueryWrapper<Plate>()
                        .eq(Plate::getId,plateId)
                        .eq(Plate::getIsDelete,BoolEnum.FALSE.getValue()));
    }

    @Override
    public boolean update(Plate plate) {
        plate.setUpdateTime(new Date());
        return plateMapper.updateById(plate) >0;
    }

    @Override
    public List<Plate> selectListByIds(List<Long> plateIds) {
        List<Plate> plateList = null;
        if(CollectionUtils.isNotEmpty(plateIds)){
            plateList=plateMapper.selectList(new LambdaQueryWrapper<Plate>().in(Plate::getId,plateIds));
        }
        return plateList!=null ? plateList : new ArrayList<>(0);
    }

	@Override
	public List<Plate> getPlateList(Long experimentId) {
		return plateMapper.selectList(
                new LambdaQueryWrapper<Plate>()
                        .eq(Plate::getExperimentId,experimentId)
                        .eq(Plate::getIsDelete,BoolEnum.FALSE.getValue()));
	}
	@Override
	public int getPlateCount(Long experimentId) {
		return plateMapper.selectCount(
				new LambdaQueryWrapper<Plate>()
				.eq(Plate::getExperimentId,experimentId)
				.eq(Plate::getIsDelete,BoolEnum.FALSE.getValue()));
	}

    @Override
    public List<Plate> selectListByExperimentIdAndType(Long experimentId,Integer type) {
        return plateMapper.selectList(
                new LambdaQueryWrapper<Plate>()
                        .eq(Plate::getExperimentId,experimentId)
                        .eq(Plate::getType,type)
                        .eq(Plate::getIsDelete,BoolEnum.FALSE.getValue()));
    }

    @Override
    public List<Plate> selectListByExperimentId(Long experimentId) {
        return plateMapper.selectList(
                new LambdaQueryWrapper<Plate>()
                        .eq(Plate::getExperimentId,experimentId)
                        .eq(Plate::getIsDelete,BoolEnum.FALSE.getValue()));
    }
}
